<?php


namespace app\cms\model\merchant\goods;


use app\cms\community\lib\QueryModel;
use app\cms\model\merchant\merchantModel;
use iflow\Response;
use think\model\relation\HasOne;

class goodsModel extends QueryModel
{

    protected $table = "goods_table";

    // 商户关联
    public function hasMerchantShop(): HasOne
    {
        return $this->hasOne(merchantModel::class, 'merchant_id', 'merchant_id');
    }

    // 商品suk关联
    public function hasGoodsSuk(): HasOne
    {
        return $this->hasOne(goodsSukModel::class, 'goods_id', 'goods_id');
    }

    // 商品类目关联
    public function hasGoodsGroup(): HasOne
    {
        return $this->hasOne(goodsGroupModel::class, 'goods_group_id', 'goods_group_id');
    }

    /**
     * 获取单个商品信息
     * @param int $goods_id
     * @return Response
     */
    public function getGoodsInfo(int $goods_id): Response
    {
        $goods = $this->withJoin([
            // 所属商户
            'hasMerchantShop' => [
                'merchant_name' => 'merchant_name',
                'merchant_logo' => 'merchant_logo',
                'merchant_hgraph' => 'merchant_hgraph',
                'merchant_desc' => 'merchant_desc',
                'merchant_address' => 'merchant_address',
                'lng' => 'lng',
                'lat' => 'lat',
                'merchant_type' => 'merchant_type'
            ],
            // 所属商品类目
            'hasGoodsGroup' => [
                'goods_group_id' => 'goods_group_id',
                'goods_group_name' => 'goods_group_name',
                'goods_group_parent_id' => 'goods_group_parent_id'
            ]
        ], 'LEFT') -> where([
            'goods_table.status' => 1,
            'goods_table.goods_id' => $goods_id
        ]) -> findOrEmpty();
        if ($goods -> isEmpty()) {
            return message() -> nodata('暂无商品信息');
        }

        $goods['suk'] = (new goodsSukModel()) -> getGoodsSuk($goods_id);
        return message() -> success('获取商品信息成功', $goods);
    }

    /**
     * 获取商品列表
     * @param string $goods_name 商品名称
     * @param int $goods_group_id 商品类目
     * @param int $merchant_id 商户id
     * @return Response
     */
    public function getGoodsList(
        string $goods_name = '', int $goods_group_id = 0, int $merchant_id = 0
    ): Response {
        $query = $this->withJoin([
            // 所属商户
            'hasMerchantShop' => [
                'merchant_name' => 'merchant_name',
                'merchant_logo' => 'merchant_logo',
                'merchant_hgraph' => 'merchant_hgraph',
                'merchant_desc' => 'merchant_desc',
                'merchant_address' => 'merchant_address',
                'lng' => 'lng',
                'lat' => 'lat',
                'merchant_type' => 'merchant_type'
            ],
            // 所属商品类目
            'hasGoodsGroup' => [
                'goods_group_id' => 'goods_group_id',
                'goods_group_name' => 'goods_group_name',
                'goods_group_parent_id' => 'goods_group_parent_id'
            ]
        ], 'LEFT') -> where([
            'goods_table.status' => 1
        ]);

        if ($goods_name !== '') {
            $query -> whereLike('goods_table.goods_name', "%${goods_name}%");
        }

        if ($goods_group_id > 0) {
            $query -> where([
                'goods_table.goods_group_id' =>  $goods_group_id
            ]);
        }

        if ($merchant_id > 0) {
            $query -> where([
                'goods_table.merchant_id' =>  $merchant_id
            ]);
        }

        return $this->paginateQuery($query);
    }

    /**
     * 上下架 商品信息
     * @param int $goods_id
     * @param string $ban_goods_desc
     * @return Response
     */
    public function banOrUnblockMerchShop(
        int $goods_id, string $ban_goods_desc
    ): Response {
        $goods = $this->where([
            'goods_id' => $goods_id
        ]) -> findOrEmpty();

        if ($goods -> isEmpty()) return message() -> nodata('暂未获取到需要更新商品的信息');
        $status = $goods['status'] === '0' ? 1 : 0;

        return $this->updateInfo([
            'status' => $status,
            'goods_ban_desc' => $ban_goods_desc
        ], [
            'goods_id' => $goods_id
        ]);
    }
}